<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import { debounce } from "@/utils";

export default {
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "300px"
    },
    pieData: Object
  },
  data() {
    return {
      chart: null
    };
  },
  mounted() {
    this.initChart();

    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize();
      }
    }, 100);
    window.addEventListener("resize", this.__resizeHandler);
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    window.removeEventListener("resize", this.__resizeHandler);
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      // console.log('el',this.$el);
      this.chart = echarts.init(this.$el, "macarons");
      let baseOption = {
        color: ["#ffdb5c", "#37a2da", "#67e0e3", "#ff9f7f", "#e062ae"],
        title: {
          backgroundColor: "#849fe2",
          textStyle: {
            color: "#fff",
            fontSize: 16
          }
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b}: {c} ({d}%)"
        },
        legend: {
          x: "center",
          bottom: "10"
        },
        serires: [
          {
            itemStyle: {
              normal: {
                color: "#ffdb5c",
                shadowColor: "#37a2da",
                label: {
                  show: false
                },
                labelLine: {
                  show: false
                }
              }
            }
          }
        ]
      };
      let option = {
        title: {
          backgroundColor: "#849fe2",
          textStyle: {
            color: "#fff",
            fontSize: 16
          }
        },
        serires: [
          {
            itemStyle: {
              normal: {
                color: "#ffdb5c",
                shadowColor: "#37a2da",
                label: {
                  show: false
                },
                labelLine: {
                  show: false
                }
              }
            }
          }
        ],
        legend:{
          show:true,
          itemGap:12,
        }
      };
      this.chart.setOption(option);
      this.chart.setOption(this.pieData);
    }
  }
};
</script>
